package com.android.jiradroid;

import com.android.jiradroid.R;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.EditTextPreference;
import android.preference.ListPreference;
import android.preference.PreferenceActivity;
import android.preference.PreferenceManager;

public class Preferences extends PreferenceActivity implements
	SharedPreferences.OnSharedPreferenceChangeListener {
	
	EditTextPreference jiraurlPreference, usernamePreference;
	ListPreference intervalPreference;
	
	@Override
	public final void onCreate(final Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		this.addPreferencesFromResource(R.xml.prefs);
		
		SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
		
		jiraurlPreference = (EditTextPreference)getPreferenceScreen().findPreference("jiraurl");
		jiraurlPreference.setSummary(sharedPreferences.getString("jiraurl", ""));
		
		usernamePreference = (EditTextPreference)getPreferenceScreen().findPreference("username");
		usernamePreference.setSummary(sharedPreferences.getString("username", ""));
	}
	
	@Override 
	protected void onResume() { 
		super.onResume();
		// Set up a listener whenever a key changes             
		getPreferenceScreen().getSharedPreferences().registerOnSharedPreferenceChangeListener(this); 
	} 
	 
	@Override 
	protected void onPause() { 
		super.onPause(); 
		// Unregister the listener whenever a key changes             
		getPreferenceScreen().getSharedPreferences().unregisterOnSharedPreferenceChangeListener(this);     
	} 

	@Override
	public final void onSharedPreferenceChanged(
		final SharedPreferences sharedPreferences, final String key) {
		if (key.equals("jiraurl")) { 
            jiraurlPreference.setSummary(sharedPreferences.getString(key, "")); 
        }
		if (key.equals("username")) { 
            usernamePreference.setSummary(sharedPreferences.getString(key, "")); 
        }
	}
}